<template>
	<view class="forget">
		<view>
			<u-toast ref="uToast" />
		</view>
		<h2>找回密码</h2>
		<!-- 手机号码 -->
		<label>
			<view class="text">
				手机号码：
			</view>
			<view class="phone">
				<view class="ipt">
					<u-input v-model="phone" type="text" placeholder-style="color:#C7C7C7;font-size: 24rpx"
						placeholder="请输入手机号" maxlength="11" :clearable="false" />
				</view>
				<view class="send-code" v-if="getCode" @click="sendCode">
					发送验证码
				</view>
				<view class="wait" v-else>
					重新发送({{time}})
				</view>

			</view>
		</label>

		<!-- 验证码 -->
		<label>
			<view class="text">
				验证码：
			</view>
			<view class="codes">
				<u-input v-model="code" type="text" placeholder-style="color:#C7C7C7;font-size: 24rpx"
					placeholder="请输入验证码" />
			</view>
		</label>
		<!-- 密码 -->
		<label>
			<view class="text">
				新密码：
			</view>
			<view class="password">
				<u-input v-model="password" type="password" placeholder-style="color:#C7C7C7;font-size: 24rpx"
					placeholder="请输入6位或以上密码" :clearable="false" />
			</view>
		</label>

		<label>
			<view class="text">
				确认密码：
			</view>
			<view class="password">
				<u-input v-model="newPassword" type="password" placeholder-style="color:#C7C7C7;font-size: 24rpx"
					placeholder="请再次输入密码" :clearable="false" />
			</view>
		</label>

		<!-- 提交按钮 -->
		<view @click="register">
			<button class="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import request from '@/util/request.js'
	import {
		phoneValidator,
		pwdValidator,
		codeValidator
	} from '@/util/validator.js'
	export default {
		data() {
			return {
				time: 60,
				password: '',
				newPassword: '',
				code: '',
				phone: '',
				getCode: true,
				timer: null
			}
		},
		methods: {
			show(title, type, callback) {
				this.$refs.uToast.show({
					title,
					type,
					duration: 3000,
					position: "top",
					callback: callback ? callback : () => {}
				})
			},
			sendCode() {

				if (!phoneValidator(this.phone, this.show)) return
				// 发送验证码
				request.globalRequest({
					url: '/api/public/code/sms/send',
					method: "post",
					data: {
						phone: this.phone,
						timestamp: new Date().getTime()
					}
				}).then(res => {
					console.log('res==>', res);
					if (res.status == "0") {
						this.show("验证码发送成功", "success")
						this.getCode = false
						this.timer = setInterval(() => {
							this.time--
							if (this.time == 0) {
								clearInterval(this.timer)
								this.getCode = true
								this.time = 60
							}
						}, 1000)
					}
				})
			},
			// 找回密码
			register() {
				//验证手机号
				if (!phoneValidator(this.phone, this.show)) return
				// 验证验证码
				if (!codeValidator(this.code, this.show)) return
				//密码验证
				if (!pwdValidator(this.password, this.show)) return
				// 验证两次输入的密码是否相同
				if (!this.password == this.newPassword) {
					return this.show("两次输入的密码不一致", 'error')
				}
				// 注册账号
				request.globalRequest({
					url: '/api/public/member/get_password',
					method: "post",
					data: {
						password: this.password,
						phone: this.phone,
						timestamp: new Date().getTime(),
						code: this.code
					}
				}).then(res => {
					console.log(res);
					if (res.status == "0") {
						this.show("密码找回成功", "success", () => {
							// 跳转到登录页面
							uni.redirectTo({
								url: '/pages/login/login'
							})
						})
					} else {
						this.show("验证码或手机号错误", "error ")
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.forget {
		width: 100vw;
		padding: 20rpx 40rpx 0;

		h2 {
			color: #494949;
			font-size: 50rpx;
			font-weight: 500;
			margin-top: 20rpx;
			padding: 20rpx 0;
			margin-bottom: 20rpx;
		}

		label {
			color: #C7C7C7;
			font-size: 24rpx;
			display: block;
			margin-bottom: 20rpx;
		}

		.phone,
		.password,
		.codes {
			margin-top: 10rpx;
			border-radius: 999rpx;
			border: 1px solid #FC5E14;
			padding: 0 40rpx;
			line-height: 10px;

			.u-input {
				align-items: center;
			}
		}

		.phone {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.ipt {
				flex: 1;
			}

			.send-code {
				color: #000;
				font-weight: 500;
				line-height: 50rpx;
			}

		}


		.submit {
			height: 70rpx;
			line-height: 70rpx;
			color: #fff;
			font-size: 28rpx;
			border-radius: 999rpx;
			background-color: #FC5E14;
			margin-top: 80rpx;
		}
	}
</style>
